package com.sina.mysql.utils;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

/**
 * @ClassName SnowflakeIdentifierGenerator
 * @Description:
 * @Author 段浩杰
 * @Date 2018/8/22 17:37
 * @Version 1.0
 */
public class SnowflakeIdentifierGenerator implements IdentifierGenerator {


    public static final String START_TIME = "1526586230768";

    private static Snowflake snowFlake;



    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        return snowFlake.next();
    }

    public static Long next() {
        if (snowFlake == null) {
            snowFlake = new Snowflake( Long.parseLong( START_TIME ), 0L, 0L );
        }
        return snowFlake.next();
    }

    public static void setSnowFlake(Snowflake snowFlake) {
        SnowflakeIdentifierGenerator.snowFlake = snowFlake;
    }
}